/**
 * Created by haocongshun on 2016/8/25.
 */
var contextPath = "/";
$(function () {
    contextPath += window.location.pathname.split("/")[1];
    $("#toptitle").html("我的消息");
    getAll(1);
    bindAllMsg();
    bindFeedbackMsg();
    bindSystemMsg();
    bindWorkMsg();
});
/***
 * 绑定显示全部信息按钮
 */
function bindAllMsg() {
    $("#allMsg").click(function () {
       getAll(1);
    });
}
/***
 * 获取所有信息
 */
function getAll(nowpage) {
    var url = contextPath+"/message/getall.wm";
    var dataAll = {};
    dataAll.flag="getall_tea";
    $.ajaxSetup({
        async : false
    });
    $.post(url,dataAll,function (result) {
        var newMessage=""
        var MSG = [];
        for(var i = 0;i < result.getall.workMsg.length;i++){
            var ceil = {};
            var temp = result.getall.workMsg[i].work_date.replace(/-/g,'/');
            ceil.date = new Date(temp.substring(0,temp.length-2));
            ceil.content="<div class='panel king-panel1 m25'>"+
                "<div class='panel-heading king-panel-head1 king-panel-top1'>"+
                "<h3 class='panel-title'>作业上交通知</h3>"+
                "</div>"+
                "<div class='panel-body'>"+
                "<p>"+
                result.getall.workMsg[i].aStudent.student_name+
                "提交了"+
                result.getall.workMsg[i].aJobTask.jobTask_content+
                "的作业"+
                "</p>"+
                "<p class='messageDate'>"+temp.substring(0,temp.length-2)+"</p>"+
                "</div>"+
                "</div>";
            MSG.push(ceil);
        }
        for(var i = 0;i < result.getall.feedbackMsg.length;i++){
            var ceil = {};
            var temp = result.getall.feedbackMsg[i].feedback_time.replace(/-/g,'/');
            ceil.date = new Date(temp.substring(0,temp.length-2));
            ceil.content="<div class='panel king-panel1 m25'>"+
                "<div class='panel-heading king-panel-head1 king-panel-top1'>"+
                "<h3 class='panel-title'>作业反馈</h3>"+
                "</div>"+
                "<div class='panel-body'>"+
                "<p>"+
                result.getall.feedbackMsg[i].aWork.aStudent.student_name+
                "回复你："+
                result.getall.feedbackMsg[i].feedback_content+
                "</p>"+
                "<a href='"+contextPath+"/talk/talk.jsp?tea&id="+result.getall.feedbackMsg[i].aWork.work_id+"'>【点击查看回复】</a>"+
                "<p class='messageDate'>"+temp.substring(0,temp.length-2)+"</p>"+
                "</div>"+
                "</div>";
            MSG.push(ceil);
        }
        for(var i = 0;i < result.getall.systemMsg.length;i++){
            var ceil = {};
            var temp = result.getall.systemMsg[i].announce_date.replace(/-/g,'/');
            ceil.date = new Date(temp.substring(0,temp.length-2));
            ceil.content="<div class='panel king-panel1 m25'>"+
                "<div class='panel-heading king-panel-head1 king-panel-top1'>"+
                "<h3 class='panel-title'>系统通知</h3>"+
                "</div>"+
                "<div class='panel-body'>"+
                "<p>"+
                result.getall.systemMsg[i].systemMessage_content+
                "</p>"+
                "<p class='messageDate'>"+temp.substring(0,temp.length-2)+"</p>"+
                "</div>"+
                "</div>";
            MSG.push(ceil);
        }
        MSG.sort(function (a,b) {
            return b.date - a.date;
        });
        var pagenum = Math.ceil(MSG.length/5);
        var pageHtml = "";
        for(var i = 5*(nowpage - 1);i < ((5*nowpage)<MSG.length?(5*nowpage):MSG.length);i++){
            newMessage+=MSG[i].content;
        }
        $("#MsgAll").html(newMessage);
        for(var i = 1; i <= pagenum;i++){
            pageHtml+="<li class='"+(i==nowpage?"active":"")+"'><a href='javascript:getAll("+i+");'>"+i+"</a></li>";
        }
        var toppage = "<li class='"+(nowpage==1?"disabled":"")+"'>"+
            "<a href='javascript:getAll("+(nowpage-1)+");' aria-label='Previous'>"+
            "<span aria-hidden='true'>«</span>"+
            "</a>"+
            "</li>";
        var bottompage = "<li class='"+(nowpage==pagenum?"disabled":"")+"'>"+
            "<a href='javascript:getAll("+(nowpage+1)+");' aria-label='Next'>"+
            "<span aria-hidden='true'>»</span>"+
            "</a>"+
            "</li>";
        $("#page").html(toppage+pageHtml+bottompage);

    },"json");
}
/***\
 * 绑定获取反馈信息按钮
 */
function bindFeedbackMsg() {
    $("#feedbackMsg").click(function () {
        getFeedbackMsg(1);
    });
}
/***
 * 获取反馈信息
 */
function getFeedbackMsg(nowpage) {
    var dataFeedback = {};
    dataFeedback.flag = 'feedback_tea';
    var url = contextPath+"/message/getFeedback.wm"
    $.post(url,dataFeedback,function (result) {
        var MSG = [];
        for(var i = 0;i < result.feedbackMsg.length;i ++){
            var ceil = {};
            var temp = result.feedbackMsg[i].feedback_time.replace(/-/g,'/');
            ceil.date = new Date(temp.substring(0,temp.length-2));
            ceil.content="<div class='panel king-panel1 m25'>"+
                "<div class='panel-heading king-panel-head1 king-panel-top1'>"+
                "<h3 class='panel-title'>作业反馈</h3>"+
                "</div>"+
                "<div class='panel-body'>"+
                "<p>"+
                result.feedbackMsg[i].aWork.aStudent.student_name+
                "回复你："+
                result.feedbackMsg[i].feedback_content+
                "</p>"+
                "<a href='"+contextPath+"/talk/talk.jsp?tea&id="+result.feedbackMsg[i].aWork.work_id+"'>【点击查看回复】</a>"+
                "<p class='messageDate'>"+temp.substring(0,temp.length-2)+"</p>"+
                "</div>"+
                "</div>";
            MSG.push(ceil);
        }
        var newMessage = "";
        //分页操作
        var pagenum = Math.ceil(MSG.length/5);
        var pageHtml = "";
        for(var i = 5*(nowpage - 1);i < ((5*nowpage)<MSG.length?(5*nowpage):MSG.length);i++){
            newMessage+=MSG[i].content;
        }
        $("#MsgFeedback").html(newMessage);
        for(var i = 1; i <= pagenum;i++){
            pageHtml+="<li class='"+(i==nowpage?"active":"")+"'><a href='javascript:getFeedbackMsg("+i+");'>"+i+"</a></li>";
        }
        var toppage = "<li class='"+(nowpage==1?"disabled":"")+"'>"+
            "<a href='javascript:getFeedbackMsg("+(nowpage-1)+");' aria-label='Previous'>"+
            "<span aria-hidden='true'>«</span>"+
            "</a>"+
            "</li>";
        var bottompage = "<li class='"+(nowpage==pagenum?"disabled":"")+"'>"+
            "<a href='javascript:getFeedbackMsg("+(nowpage+1)+");' aria-label='Next'>"+
            "<span aria-hidden='true'>»</span>"+
            "</a>"+
            "</li>";
        $("#pageFeedback").html(toppage+pageHtml+bottompage);
        
    },"json");
}
/***
 * 绑定获取系统信息按钮
 */
function bindSystemMsg() {
    $("#systemMsg").click(function () {
        getSystemMsg(1);
    });
}
/***
 * 获取系统信息
 */
function getSystemMsg(nowpage) {
    var dataSystem = {};
    url = contextPath+"/message/getSystem.wm"
    $.post(url,dataSystem,function (result) {
        var MSG = [];
        for(var i = 0;i < result.systemMsg.length;i ++){
            var ceil = {};
            var temp = result.systemMsg[i].announce_date.replace(/-/g,'/');
            ceil.date = new Date(temp.substring(0,temp.length-2));
            ceil.content="<div class='panel king-panel1 m25'>"+
                "<div class='panel-heading king-panel-head1 king-panel-top1'>"+
                "<h3 class='panel-title'>系统通知</h3>"+
                "</div>"+
                "<div class='panel-body'>"+
                "<p>"+
                result.systemMsg[i].systemMessage_content+
                "</p>"+
                "<p class='messageDate'>"+temp.substring(0,temp.length-2)+"</p>"+
                "</div>"+
                "</div>";
            MSG.push(ceil);
        }
        var newMessage = "";
        //分页操作
        var pagenum = Math.ceil(MSG.length/5);
        var pageHtml = "";
        for(var i = 5*(nowpage - 1);i < ((5*nowpage)<MSG.length?(5*nowpage):MSG.length);i++){
            newMessage+=MSG[i].content;
        }
        $("#MsgSystem").html(newMessage);
        for(var i = 1; i <= pagenum;i++){
            pageHtml+="<li class='"+(i==nowpage?"active":"")+"'><a href='javascript:getSystemMsg("+i+");'>"+i+"</a></li>";
        }
        var toppage = "<li class='"+(nowpage==1?"disabled":"")+"'>"+
            "<a href='javascript:getSystemMsg("+(nowpage-1)+");' aria-label='Previous'>"+
            "<span aria-hidden='true'>«</span>"+
            "</a>"+
            "</li>";
        var bottompage = "<li class='"+(nowpage==pagenum?"disabled":"")+"'>"+
            "<a href='javascript:getSystemMsg("+(nowpage+1)+");' aria-label='Next'>"+
            "<span aria-hidden='true'>»</span>"+
            "</a>"+
            "</li>";
        $("#pageSystem").html(toppage+pageHtml+bottompage);

    },"json");
}
/***
 * 绑定获取上交作业通知的按钮
 */
function bindWorkMsg() {
    $("#workMsg").click(function () {
        getWorkMsg(1);
    });
}
/***
 * 获取作业上交通知
 */
function getWorkMsg(nowpage) {
    var dataWork = {};
    var url = contextPath+"/message/getWork.wm";
    $.post(url,dataWork,function (result) {
        var newMessage=""
        var MSG = [];
        for(var i = 0;i < result.workMsg.length;i++){
            var ceil = {};
            var temp = result.workMsg[i].work_date.replace(/-/g,'/');
            ceil.date = new Date(temp.substring(0,temp.length-2));
            ceil.content="<div class='panel king-panel1 m25'>"+
                "<div class='panel-heading king-panel-head1 king-panel-top1'>"+
                "<h3 class='panel-title'>作业上交通知</h3>"+
                "</div>"+
                "<div class='panel-body'>"+
                "<p>"+
                result.workMsg[i].aStudent.student_name+
                "提交了"+
                result.workMsg[i].aJobTask.jobTask_content+
                "的作业"+
                "</p>"+
                "<p class='messageDate'>"+temp.substring(0,temp.length-2)+"</p>"+
                "</div>"+
                "</div>";
            MSG.push(ceil);
        }
        //分页操作
        var pagenum = Math.ceil(MSG.length/5);
        var pageHtml = "";
        for(var i = 5*(nowpage - 1);i < ((5*nowpage)<MSG.length?(5*nowpage):MSG.length);i++){
            newMessage+=MSG[i].content;
        }
        $("#MsgWork").html(newMessage);
        for(var i = 1; i <= pagenum;i++){
            pageHtml+="<li class='"+(i==nowpage?"active":"")+"'><a href='javascript:getWorkMsg("+i+");'>"+i+"</a></li>";
        }
        var toppage = "<li class='"+(nowpage==1?"disabled":"")+"'>"+
            "<a href='javascript:getWorkMsg("+(nowpage-1)+");' aria-label='Previous'>"+
            "<span aria-hidden='true'>«</span>"+
            "</a>"+
            "</li>";
        var bottompage = "<li class='"+(nowpage==pagenum?"disabled":"")+"'>"+
            "<a href='javascript:getWorkMsg("+(nowpage+1)+");' aria-label='Next'>"+
            "<span aria-hidden='true'>»</span>"+
            "</a>"+
            "</li>";
        $("#pageWork").html(toppage+pageHtml+bottompage);
        
        
    },"json");

}